package com.atguigu.spzx.manager.service.impl;

import com.atguigu.spzx.manager.mapper.BrandMapper;
import com.atguigu.spzx.manager.service.BrandService;
import com.atguigu.spzx.manager.service.FileService;
import com.atguigu.spzx.model.entity.brand.Brand;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * title:
 * author: zgl
 * date: 2024/1/3
 * description：
 */
@Service
public class BrandServiceImpl implements BrandService {
    @Autowired
    private BrandMapper brandMapper;

    @Autowired
    private FileService fileService;

    @Override
    public PageInfo<Brand> getBrandList(Integer pageNum, Integer pageSize) {
        //设置分页参数
        PageHelper.startPage(pageNum, pageSize);
        //查询数据库
        List<Brand> brandList = brandMapper.getBrandList();
        return new PageInfo<>(brandList);
    }

    @Override
    public Brand getBrandById(Long id) {
        return brandMapper.getBrandById(id);
    }

    @Override
    public void deletedBrand(Long id) {
        //根据id查询品牌信息
        Brand brand = brandMapper.getBrandById(id);
        brandMapper.deletedBrand(id);
        if (!StringUtils.isEmpty(brand.getLogo())) {
            fileService.delete(brand.getLogo());
        }
    }

    @Override
    public void updateBrand(Brand brand) {
        brandMapper.updateBrand(brand);
    }

    @Override
    public void saveBrand(Brand brand) {
        brandMapper.saveBrand(brand);
    }

    @Override
    public List<Brand> list() {
        return brandMapper.getBrandList();
    }
}
